package cn.iocoder.yudao.module.system.controller.admin.casedata;

import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.module.system.controller.admin.casedata.vo.CasePageReqVO;
import cn.iocoder.yudao.module.system.controller.admin.casedata.vo.CaseRespVO;
import cn.iocoder.yudao.module.system.controller.admin.casedata.vo.CaseSaveReqVO;
import cn.iocoder.yudao.module.system.dal.dataobject.casedata.CaseDO;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.casedata.CaseService;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 案件")
@RestController
@RequestMapping("/system/case")
@Validated
public class CaseController {

    @Resource
    private CaseService caseService;
    @Resource
    private AdminUserService userService;

    @PostMapping("/create")
    @Operation(summary = "创建案件")
//    @PreAuthorize("@ss.hasPermission('system:case:create')")
    public CommonResult<Long> createCase(@RequestBody CaseSaveReqVO createReqVO) {
        return success(caseService.createCase(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新案件")
//    @PreAuthorize("@ss.hasPermission('system:case:update')")
    public CommonResult<Boolean> updateCase( @RequestBody CaseSaveReqVO updateReqVO) {
        caseService.updateCase(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除案件")
    @Parameter(name = "id", description = "编号", required = true)
//    @PreAuthorize("@ss.hasPermission('system:case:delete')")
    public CommonResult<Boolean> deleteCase(@RequestParam("id") Long id) {
        caseService.deleteCase(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得案件")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('system:case:query')")
    public CommonResult<CaseRespVO> getCase(@RequestParam("id") Long id) {
        CaseDO caseData = caseService.getCase(id);
        if(ObjectUtil.isNotNull(caseData)){
            AdminUserDO user = userService.getUser(caseData.getReviewerBy());
            if(ObjectUtil.isNotNull(user)){
            caseData.setReviewer(user.getUsername());
            }
        }
        return success(BeanUtils.toBean(caseData, CaseRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得案件分页")
//    @PreAuthorize("@ss.hasPermission('system:case:query')")
    public CommonResult<PageResult<CaseRespVO>> getCasePage(CasePageReqVO pageReqVO) {
        PageResult<CaseDO> pageResult = caseService.getCasePage(pageReqVO);
        if (!pageResult.getList().isEmpty()) {
//            List<Long>reviewerBys=pageResult.getList().stream().map(CaseDO::getReviewerBy).distinct().collect(Collectors.toList());
//            Map<Long, AdminUserDO> userMap = userService.getUserMap(reviewerBys);
            pageResult.getList().forEach(e->{
//                  AdminUserDO user = userMap.get(e.getReviewerBy());
//                AdminUserDO user = userService.selectByIdOne(e.getReviewerBy());
                AdminUserDO user = userService.getUser(e.getReviewerBy());
                if(ObjectUtil.isNotNull(user)){
                    e.setReviewer(user.getUsername());
                }

            });

        }

        return success(BeanUtils.toBean(pageResult, CaseRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出案件 Excel")
//    @PreAuthorize("@ss.hasPermission('system:case:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportCaseExcel(@Valid CasePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CaseDO> list = caseService.getCasePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "案件.xls", "数据", CaseRespVO.class,
                        BeanUtils.toBean(list, CaseRespVO.class));
    }

}